package com.ruanjian.reggie.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanjian.reggie.dto.DishDto;
import com.ruanjian.reggie.entity.Dish;
import com.ruanjian.reggie.entity.DishFlavor;
import com.ruanjian.reggie.mapper.DishFlavorMapper;
import com.ruanjian.reggie.mapper.DishMapper;
import com.ruanjian.reggie.service.CategoryService;
import com.ruanjian.reggie.service.DishFlavorService;
import com.ruanjian.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {
    
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    CategoryService categoryService;

    @Autowired
    private DishMapper dishMapper;

    @Transactional
    public void saveDishAndDishFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors= flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        /**
         * 两种写法都可以实现视频中使用的是上面这种
         * */
//        for (DishFlavor dishFlavor:flavors) {
//            dishFlavor.setDishId(dishId);
//        }
        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public IPage<List<Map<String, Object>>> queryListPage(int page,int pageSize,String name) {
        IPage<List<Map<String,Object>>> iPage=new Page<>(page,pageSize);
        return dishMapper.queryListPage(iPage,name);
    }

    @Override
    public void updateDishAndFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,dishDto.getId()));
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public List<Dish> getDishList(String categoryId) {
        List<Dish> dishes = dishMapper.selectList(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, categoryId));
        return dishes;
    }
    @Override
    public List<DishDto> getDishDtoList(Dish dish) {
        List<Dish> dishes = dishMapper.selectList(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, dish.getCategoryId()));
        List<DishDto> collect = dishes.stream().map((item) -> {
            //这里出现了一个bug是因为DishDto对象创建在了外面使用的是一个对象所以菜品展示只有一种结果
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long dishId = item.getId();
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dishId));
            if (dishFlavors != null) {
                dishDto.setFlavors(dishFlavors);
            }
            return dishDto;
        }).collect(Collectors.toList());
        return collect;
    }
}
